<?php

namespace App\Exceptions;

use App\Exceptions\BaseException;
use App\Exceptions\WeChatException;
use Request;
use Response;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        HttpException::class,
        ModelNotFoundException::class,
        BaseException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        return parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }

        if($e instanceof WeChatException) {
            $msg = explode(' ', $e->getMessage());

            if(reset($msg) == 40001){
                session()->forget('userId');
                return redirect('/index');
            }
        }
        if(!env('APP_DEBUG')){
            if ($e instanceof BaseException){
                if(Request::ajax() || Request::wantsJson()){
                    return Response::json($e);
                }else if(!$request->is('admin/*')){
                    if($e->getCode() == 20005){
                        return Response::make(view('bind', wxConfig(['status' => ["message" => $e->getMessage()]], $request)));
                    }else if($request->is('api/wechat-pay-notify')){
                        return Response::json($e);
                    }
                    $state['deviceCode'] = deviceCode();
                    return Response::make(view('index', wxConfig([
                        'status' => config('exceptions.device.notConnect'),
                        'state' => $state,
                        'host' => head($request->header()['host']),
                        ], $request)));
                }else if($request->is('admin/*')){
                    if($e->getCode() == 1) {
                        return response()->view('admin/exportErrorPage', ['message' => $e->getMessage()]);
                        // return Response::make(view('admin/errors/hint', ['status' => config('exceptions.user.admin')]));
                    }
                }
                return back()->with(['status' => ['errCode' => $e->getCode(), 'message' => $e->getMessage()]]);
            }
        }

        return parent::render($request, $e);
    }
}
